package com.file.system.controller.system;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.system.oshi.CpuInfo;
import com.file.system.entity.res.CommonResult;
import com.file.system.entity.info.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import static cn.hutool.system.SystemUtil.getRuntimeInfo;
import static cn.hutool.system.oshi.OshiUtil.*;

/**
 * 系统信息
 *
 * @author 靳春晖
 */
@RestController
@RequestMapping("/sys")
public class SystemController {


    /**
     * 系统信息
     *
     * @return CommonResult
     * @author 靳春晖
     */
    @PostMapping("/configure")
    public CommonResult<SysConfigure> configure() {
        SysConfigure configure = new SysConfigure();
        //cpu---------------------------------------------------------------------------------------------------------------------------------
        CpuInfo cpuInfo = getCpuInfo();
        Cpu cpu = new Cpu();
        cpu.setCpuNum(cpuInfo.getCpuNum());
        cpu.setSysPer(cpuInfo.getSys());
        cpu.setUserPer(cpuInfo.getUser());
        cpu.setUsedPer(cpuInfo.getUsed());
        cpu.setFreePer(cpuInfo.getFree());
        cpu.setCpuModel(cpuInfo.getCpuModel());
        // 获取CPU温度
        cpu.setTemperature(getSensors().getCpuTemperature());
        //内存---------------------------------------------------------------------------------------------------------------------------------
        Memory memory = new Memory();
        memory.setMaxMemory(getRuntimeInfo().getMaxMemory());
        memory.setTotalMemory(getRuntimeInfo().getTotalMemory());
        memory.setFreeMemory(getRuntimeInfo().getFreeMemory());
        memory.setUsableMemory(getRuntimeInfo().getUsableMemory());
        //服务器信息----------------------------------------------------------------------------------------------------------------------------
        Server server = new Server();
        Properties properties = System.getProperties();
        List<NetWork> netWorkList = new ArrayList<>();
        for (int i = 0; i < getNetworkIFs().size(); i++) {
            NetWork netWork = new NetWork();
            netWork.setIPv4(getNetworkIFs().get(i).getIPv4addr().length > 0 ? getNetworkIFs().get(i).getIPv4addr()[0] : null);
            netWork.setIPv6(getNetworkIFs().get(i).getIPv6addr().length > 0 ? getNetworkIFs().get(i).getIPv6addr()[0] : null);
            netWorkList.add(netWork);
        }
        server.setNetWorks(netWorkList);
        server.setOSName(getOs().getFamily());
        server.setManufacturer(getOs().getManufacturer());
        server.setOSArch(String.valueOf(properties.get("os.arch")));
        //虚拟机信息----------------------------------------------------------------------------------------------------------------------------
        JavaJVM javaJVM = new JavaJVM();
        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
        javaJVM.setName(bean.getVmName());
        javaJVM.setVersion(bean.getSpecVersion());
        javaJVM.setPath(System.getProperty("java.home"));
        javaJVM.setProjectPath(System.getProperty("user.dir"));
        javaJVM.setStartTime(DateUtil.format(new Date(bean.getStartTime()), "yyyy-MM-dd HH:mm:ss"));
        javaJVM.setRunHour(DateUtil.between(new Date(bean.getStartTime()), new Date(), DateUnit.HOUR));
        //磁盘---------------------------------------------------------------------------------------------------------------------------------
        File[] disks = File.listRoots();
        List<Disk> list = new ArrayList<>();
        for (File file : disks) {
            Disk disk = new Disk();
            disk.setName(file.getPath().replaceAll("\\\\", "").replaceAll(":", ""));
            disk.setNotUsed(file.getFreeSpace());
            disk.setAvailable(file.getUsableSpace());
            disk.setUsed(file.getTotalSpace() - file.getUsableSpace());
            disk.setTotal(file.getTotalSpace());
            list.add(disk);
        }

        configure.setCpu(cpu);
        configure.setDisks(list);
        configure.setJavaJVM(javaJVM);
        configure.setMemory(memory);
        configure.setServer(server);
        return CommonResult.ok().setResult(configure);

    }


}
